<template>
  <div class="login" @keydown.enter="handleSubmit">
    <div class="login-con">
      <Card :bordered="false">
        <p slot="title">
          <Icon type="log-in"></Icon>
          欢迎登录
        </p>
        <div class="form-con">
          <Form ref="loginForm" :model="ruleForm" :rules="rules">
            <FormItem prop="loginName">
              <Input v-model="ruleForm.loginName" placeholder="请输入用户名">
                <span slot="prepend"><Icon :size="16" type="person"></Icon></span>
              </Input>
            </FormItem>
            <FormItem prop="password">
              <Input type="password" v-model="ruleForm.password" placeholder="请输入密码">
                <span slot="prepend"> <Icon :size="14" type="locked"></Icon> </span>
              </Input>
            </FormItem>
            <FormItem>
              <Button @click="handleSubmit" type="primary" size="large" long :loading="loading">{{loginBtn}}</Button>
            </FormItem>
          </Form>
        </div>
      </Card>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
  import Vue from 'vue';
  import {SERVICE_URLS} from "../../libs/service.url";

  export default {
    components: {},
    data() {
      return {
        loginBtn : `登 录`,
        loading: false,
        ruleForm: {
          loginName: '',
          password: '',
          rememberMe : false
        },
        rules: {
          loginName: [
            {required: true, message: '账号不能为空！', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '密码不能为空！', trigger: 'blur'}
          ]
        }
      }
    },
    created(){

    },
    methods: {
      handleSubmit () {
        let self = this;
        this.$refs.loginForm.validate((valid) => {
          if (valid) {
            this.loading = true;
            this.loginBtn = `正在登录`;
            Vue.submit(self, {
              loading: 'loading',
              url: SERVICE_URLS.common.login,
              data: {
                loginName: self.ruleForm.loginName,
                password: sha512(self.ruleForm.password)  // 加密sha512(self.ruleForm.password)
              },
              callback: (_data) => {
                self.$Message.success('登录成功!');
                localStorage.setItem('currentUser', JSON.stringify(_data));
                self.$store.commit('setCurrentUser',_data);
                self.$router.push({path: '/culture/about'});
              },
              error:(err) => {
                self.$Message.error(err);
                self.loading = false;
                self.loginBtn = `登 录`;
              }
            })
          }
        });
      }
    }
  }
</script>

<style scoped lang="scss">

</style>
